package com.hgyc.mom.eam.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgyc.mom.common.exception.BizException;
import com.hgyc.mom.eam.entity.EamMaintainReport;
import com.hgyc.mom.eam.entity.EamMaintainReportLine;
import com.hgyc.mom.eam.mapper.EamMaintainReportMapper;
import com.hgyc.mom.eam.service.EamMaintainReportLineService;
import com.hgyc.mom.eam.service.EamMaintainReportService;
import com.hgyc.mom.eam.vo.EamMaintainReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 设备保养记录Service业务层实现
 *
 * @author fwj
 * @since 2025-08-16
 */
@Slf4j
@Service
public class EamMaintainReportServiceImpl extends ServiceImpl<EamMaintainReportMapper, EamMaintainReport> implements EamMaintainReportService {

    @Autowired
    private EamMaintainReportLineService eamMaintainReportLineService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveReport(EamMaintainReportVO eamMaintainReportVO) {
        // TODO 其他逻辑
        save(eamMaintainReportVO);

        //新增设备保养记录明细
        List<EamMaintainReportLine> lines = eamMaintainReportVO.getAddLines();
        if (!CollectionUtils.isEmpty(lines)) {
            lines.forEach(item -> {
                item.setId(null);
                item.setReportId(eamMaintainReportVO.getId());
            });
            eamMaintainReportLineService.saveBatch(lines);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateReport(EamMaintainReportVO eamMaintainReportVO) {
        // TODO 其他逻辑
        updateById(eamMaintainReportVO);

        //删除设备保养记录明细
        if (!CollectionUtils.isEmpty(eamMaintainReportVO.getDeleteLineIds())) {
            eamMaintainReportLineService.removeBatchByIds(eamMaintainReportVO.getDeleteLineIds());
        }

        //新增设备保养记录明细
        List<EamMaintainReportLine> lines = eamMaintainReportVO.getAddLines();
        if (!CollectionUtils.isEmpty(lines)) {
            lines.forEach(item -> {
                item.setId(null);
                item.setReportId(eamMaintainReportVO.getId());
            });
            eamMaintainReportLineService.saveBatch(lines);
        }

        //更新设备保养记录明细
        if (!CollectionUtils.isEmpty(eamMaintainReportVO.getUpdateLines())) {
            List<EamMaintainReportLine> updateLines = eamMaintainReportVO.getUpdateLines();
            eamMaintainReportLineService.updateBatchById(updateLines);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeReport(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除设备保养记录明细
        eamMaintainReportLineService.deleteBatchByParentIds(ids);
    }

    @Override
    public void updateStatus(String status, List<Long> ids) {
        LambdaUpdateWrapper<EamMaintainReport> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(EamMaintainReport::getId, ids);
        updateWrapper.set(EamMaintainReport::getStatus, status);
        update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<EamMaintainReport> eamMaintainReportList, boolean updateSupport) {
        if (eamMaintainReportList == null || eamMaintainReportList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + eamMaintainReportList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + eamMaintainReportList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("设备保养记录导入失败", e);
            throw new BizException("设备保养记录导入失败：" + e.getMessage());
        }
    }
}
